﻿
using Xacml.Core;
using System;
using System.Xml;

namespace Xacml.Core.Policy
{
  public class AttributeAssignmentElementReadWrite : StringValueBase
  {
    private string _attributeId;
    private string _dataType;
    private string _contents;

    public override string Value
    {
      get
      {
        return this._contents;
      }
      set
      {
        this._contents = value;
      }
    }

    public override string DataTypeValue
    {
      get
      {
        return this._dataType;
      }
      set
      {
        this._dataType = value;
      }
    }

    public virtual string AttributeId
    {
      get
      {
        return this._attributeId;
      }
      set
      {
        this._attributeId = value;
      }
    }

    public override bool IsReadOnly
    {
      get
      {
        return false;
      }
    }

    public AttributeAssignmentElementReadWrite(XmlReader reader, XacmlVersion schemaVersion)
      : base(XacmlSchema.Policy, schemaVersion)
    {
      if (reader.LocalName == "AttributeAssignment" && this.ValidateSchema(reader, schemaVersion))
      {
        if (reader.HasAttributes)
        {
          while (reader.MoveToNextAttribute())
          {
            if (reader.LocalName == "DataType")
              this._dataType = reader.GetAttribute("DataType");
            else if (reader.LocalName == "AttributeId")
              this._attributeId = reader.GetAttribute("AttributeId");
          }
          reader.MoveToElement();
        }
        this._contents = reader.ReadInnerXml();
      }
      else
        throw new Exception(Resource.ResourceManager[Resource.MessageKey.exc_invalid_node_name, new object[1]
        {
          (object) reader.LocalName
        }]);
    }

    public AttributeAssignmentElementReadWrite(string attributeId, string dataType, string contents, XacmlVersion version)
      : base(XacmlSchema.Policy, version)
    {
      this._attributeId = attributeId;
      this._dataType = dataType;
      this._contents = contents;
    }

    public override int GetHashCode()
    {
      return this._contents.GetHashCode();
    }

    public override string ToString()
    {
      return "[" + this._dataType + "]:" + this._contents;
    }
  }
}
